package com.tedu.ds.sessionAndMessage.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitFanoutConfig {

    private String serverQueueName;
    private String deadQueueName;
    public String getServerQueueName(){
        return SERVER_QUEUE_PREFIX+uuid;
    }
    public String getDeadQueueName(){
        return DEAD_QUEUE_PREFIX+uuid;
    }

    // 定义常量
    public static final String FANOUT_EXCHANGE_NAME = "chat_fanout_exchange";
    public static final String DEAD_EXCHANGE_NAME = "chat_deadLetter_exchange";
    public static final String BLANK_EXCHANGE_NAME = "chat_blank_exchange";

    // 队列名称前缀
    public static final String SERVER_QUEUE_PREFIX = "server_queue-";
    public static final String DEAD_QUEUE_PREFIX = "dead_letter_queue-";
    public static final String BLANK_QUEUE_PREFIX = "blank_queue-";

    // 路由键
    public static final String DEAD_ROUTING_KEY = "chat_deadLetter_routingKey-";
    public static final String BLANK_ROUTING_KEY = "chat_blank_routingKey-";

    public static String uuid;

    // 静态初始化块，确保在类加载时获取主机名
    static {
        try {
            uuid = getServerIp();
        } catch (RuntimeException e) {
            throw new IllegalStateException("无法获取服务器IP地址", e);
        }
    }

    // 获取主机名
    private static String getServerIp() {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getHostName();  // 返回主机名
        } catch (UnknownHostException e) {
            throw new RuntimeException("无法获取服务器IP地址", e);
        }
    }

    // 消息的最大存活时间（60秒）
    private static final int MESSAGE_TTL = 60000;

    // 创建Fanout Exchange
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE_NAME, true, false);  // 持久化交换机
    }

    // 创建Blank Exchange
    @Bean
    public DirectExchange blankExchange() {
        return new DirectExchange(BLANK_EXCHANGE_NAME, true, false);  // 持久化交换机
    }

    // 创建死信交换机
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE_NAME, true, false);  // 持久化死信交换机
    }

    // 创建普通队列，并配置空白队列
    @Bean
    public Queue serverQueue() {
        String queueName = SERVER_QUEUE_PREFIX + uuid;
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", BLANK_EXCHANGE_NAME);  // 空白交换机
        args.put("x-dead-letter-routing-key", BLANK_ROUTING_KEY+uuid);  // 空白路由键
        return new Queue(queueName, true, false, false, args);  // 持久化队列
    }

    //创建空白队列，并配置死信队列
    @Bean
    public Queue blankQueue(){
        String queueName=BLANK_QUEUE_PREFIX+uuid;
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE_NAME);  // 死信交换机
        args.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY+uuid);  // 死信路由键
        args.put("x-message-ttl", MESSAGE_TTL);  // 消息的最大存活时间（60秒）
        return new Queue(queueName, true, false, false, args);  // 持久化队列
    }

    // 创建死信队列
    @Bean
    public Queue deadQueue() {
        return new Queue(DEAD_QUEUE_PREFIX +uuid, true);  // 持久化死信队列
    }

    // 将普通队列绑定到Fanout Exchange
    @Bean
    public Binding bindingServerQueueToFanoutExchange(Queue serverQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(serverQueue).to(fanoutExchange);
    }

    //将空白队列绑定到空白交换机
    @Bean
    public Binding bindingBlankQueueToBlankExchange(Queue blankQueue,DirectExchange blankExchange){
        return BindingBuilder.bind(blankQueue).to(blankExchange).with(BLANK_ROUTING_KEY+uuid);
    }

    // 将死信队列绑定到死信交换机
    @Bean
    public Binding bindingDeadQueueToDeadExchange(Queue deadQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY+uuid);
    }
}